/**
 * 							MUTEX MANAGEMENT
 * @brief	    mutex management
 * @author	    chy.
 * @note	    this file manage mutex
 * @comments	mutex need disable interrupt!
 */
 
#ifndef _K_MUTEX_DEF_C_
#define _K_MUTEX_DEF_C_

#include "../task/kernel_task_def.h"
#include "k_mutex_def.h"


/*
*                   EXTERN  FUNCTIONS
*/
void *k_mm_os_heap_allocate(unsigned int size);
void k_mm_os_heap_free(void *mem_addr);

/*
*                   FUNCTIONS
*/

/**
 * @BRIEF	:	Create Mutex
 * @PARAM	:	state   1 sigled when create, 0 unsigled!
 * @RETURN	:	handle of mutex created
 * @NOTE	:	
 */
K_HMUTEX k_mutex_create(int state)
{
    K_PMutex mutex = (K_PMutex)k_mm_os_heap_allocate(sizeof(K_Mutex));
    if(mutex == 0)
    {
        return 0;
    }
    
    if(state)
    {
        mutex->val = 0;
        K_MEM_DISABLE_INTERRUPT;
        mutex->val++;
        K_MEM_ENABLE_INTERRUPT;
    }
    
    return (unsigned int)mutex;
}

#endif  /* _K_MUTEX_DEF_C_ */
